package controllers.common.interceptor;

import org.apache.commons.lang.StringUtils;

import com.za.util.StringUtil;

import models.common.bean.CurrUser;
import models.common.entity.t_flow_switch;
import play.mvc.Before;
import play.mvc.Controller;
import services.common.FlowSwitchService;
import services.common.SettingService;
import common.annotation.CAauthentication;
import common.annotation.CompanyAssessMsg;
import common.annotation.CompanyBasicMsg;
import common.annotation.CompanyIsBackMsg;
import common.annotation.EnterpriseCertification;
import common.annotation.ExpiryTimeAnno;
import common.annotation.LoginCheck;
import common.annotation.PaymentAccountCheck;
import common.annotation.RealNameCheck;
import common.constants.ConfConst;
import common.constants.SettingKey;
import common.utils.DateToIntUtil;
import common.utils.Factory;
import common.utils.ResultInfo;
import controllers.common.FrontBaseController;

/**
 * 用户状态拦截器
 * 	 包括：登陆拦截器、资金托管账户开户拦截、用户未实名认证拦截器、未绑定银行卡
 *
 * @description 
 *
 * @author huangyunsong
 * @createDate 2016年1月15日
 */
public class UserStatusInterceptor extends Controller{
	
	protected static FlowSwitchService flowSwitchService =Factory.getService(FlowSwitchService.class);

	/** 注入系统设置service */
	protected static SettingService settingService = Factory.getService(SettingService.class);
	
	/**
	 * 登陆拦截器
	 */
	@Before(priority=11)
	static void checkLogin(){
		LoginCheck checkAction = getActionAnnotation(LoginCheck.class);
		if (checkAction == null) {
			
			return;
		}
		
		CurrUser user = FrontBaseController.getCurrUser();
		if (user != null) {
			
			return;
		}

		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_LOGIN;
			result.msg = "没有登录，或者登录状态已经失效!请重新登录!";
			
			renderJSON(result);
			
		} else{
			flash.error("请先登录!");
			redirect("front.FrontHomeCtrl.frontHomePre");
		}
	}
	
	/**
	 * 资金托管账户开户拦截
	 */
	@Before(priority=12)
	static void checkPaymentAccount(){
		PaymentAccountCheck checkAction = getActionAnnotation(PaymentAccountCheck.class);
		 return;
		 
	}
	
	/**
	 * 用户未实名认证拦截器
	 */
	@Before(priority=13)
	static void checkRealName(){
		RealNameCheck realNameCheck = getActionAnnotation(RealNameCheck.class);
		if (realNameCheck == null) {
			
			return;
		}
		
		/* 获取登录当前对象 */
		CurrUser user = FrontBaseController.getCurrUser();
		if (user == null) {
			
			checkLogin();
		}
		//如果是企业的话就不需要实名认证
		if (user.is_company == 1) {
			return;
		}
		//个人需要进行实名的认证
		if (user.is_real_name && user.is_company == 0) {
			
			return;
		}
		
		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_REAL_NAME;
			result.msg = "未实名认证";
			
			renderJSON(result);
		} else{
			flash.error("请先进行实名认证");
			redirect("front.account.MySecurityCtrl.toRealNameIdentityPre");
		}
	}
	/**
	 * 企业的电子签章认证
	 */
	@Before(priority=14)
	static void checkCompanyCA(){
		CAauthentication caCheck = getActionAnnotation(CAauthentication.class);
		if (caCheck == null) {
			
			return;
		}
		/* 获取登录当前对象 */
		CurrUser user = FrontBaseController.getCurrUser();
		if (user == null) {
			
			checkLogin();
		}else{
			return;
		}
		
		
		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_REAL_NAME;
			result.msg = "请先完成电子签章认证";
			
			renderJSON(result);
		} else{
			if (user.is_company == 1) {
				flash.error("请先完成电子签章认证");
				redirect("front.account.CompanyInfoCtrl.toCompanyBasicInfoPre");
			}else {
				flash.error("请先完成电子签章认证");
				redirect("front.account.MySecurityCtrl.toRealNameIdentityPre");
			}
		}
	}
	/**
	 * 企业认证
	 */
	@Before(priority=15)
	static void checkCompanyCer(){
		EnterpriseCertification companyCheck = getActionAnnotation(EnterpriseCertification.class);
		if (companyCheck == null) {
			return;
		}
		/* 获取登录当前对象 */
		CurrUser user = FrontBaseController.getCurrUser();
		if (user == null) {
			
			checkLogin();
		}
		if (user.is_company == 0) {
			return;
		}
		if (user.company_certif) {
			return;
		}
		
		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_REAL_NAME;
			result.msg = "未实名认证";
			renderJSON(result);
		} 
	}
	
	/**
	 * 企业基本资料拦截
	 */
	@Before(priority=16)
	static void checkCompanyBasicMsg(){
		CompanyBasicMsg combaic = getActionAnnotation(CompanyBasicMsg.class);
		if (combaic == null) {
			return;
		}
		/* 获取登录当前对象 */
		CurrUser user = FrontBaseController.getCurrUser();
		if (user == null) {
			
			checkLogin();
		}
		if (user.is_company == 0) {
			return;
		}
		if (user.basic_info_audit_status) {
			return;
		}
		
		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_REAL_NAME;
			result.msg = "未实名认证";
			renderJSON(result);
		}
	}
	/**
	 * 黑名单企业拦截
	 */
	@Before(priority=17)
	static void checkCompanyIsBackMsg(){
		CompanyIsBackMsg combaic = getActionAnnotation(CompanyIsBackMsg.class);
		if (combaic == null) {
			return;
		}
		/* 获取登录当前对象 */
		CurrUser user = FrontBaseController.getCurrUser();
		if (user == null) {
			
			checkLogin();
		}
		if (user.is_company == 0) {
			return;
		}
		if (user.is_black) {
			return;
		}
		
		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_REAL_NAME;
			result.msg = "黑名单企业无法进行此次操作！请联系平台管理员";
			renderJSON(result);
		} /*else{
			flash.error("黑名单企业无法进行此次操作！请联系平台管理员");
			redirect("front.account.CompanyInfoCtrl.toCompanyBasicInfoPre");
		}*/
	}
	
	/**
	 * 用户融通时间
	 */
	
	@Before(priority=19)
	static void  expiryTime(){
		ExpiryTimeAnno expiryTimeAnno = getActionAnnotation(ExpiryTimeAnno.class);
		if (expiryTimeAnno == null) {
			return;
		}
		String url = request.url;
		//返回状态 默认是0 0否1是
		int codeValue = 0;
		
		t_flow_switch financing=flowSwitchService.findByID(1);
		
		
		String startup_for_check_end = settingService.findSettingValueByKey(SettingKey.STARTUP_FOR_CHECK_END);
		
		String startup_for_check_start = settingService.findSettingValueByKey(SettingKey.STARTUP_FOR_CHECK_START);
		
		if("0".equals(startup_for_check_end)){
			
			startup_for_check_end = "24:00:00";
		}else{
			startup_for_check_end = startup_for_check_end +":00:00";
			
		}
		if("0".equals(startup_for_check_start)){
			
			startup_for_check_start = "24:00:00";
		}else{
			startup_for_check_start = startup_for_check_start +":00:00";
			
		}
		
		//获取当前的时分秒 结束时间
		if (startup_for_check_end != null || StringUtils.isEmpty(startup_for_check_end)) {
		    //当前开始融通的时间是否为当天账款融通结束的时间
			if (!DateToIntUtil.checkEndDateForLoal(startup_for_check_end)) {
				codeValue = 1;
			}
		}
		//获取当前的时分秒 开始时间
		if (startup_for_check_start != null || StringUtils.isEmpty(startup_for_check_start)) {
		    //当前开始融通的时间是否为当天账款融通开始时间之后
			if (!DateToIntUtil.checkStartDateForLoal(startup_for_check_start)) {
				codeValue = 1;
			}
		}
		
		//判断挂牌融通开关 0开；1关
		if(financing !=null){
			if(financing.financing_switch == 1){
				codeValue = 1;
			}
		}
		
		//判断是否可以进行融通
		if (codeValue == 0) {
			return;
		}else {
			redirect("front.LoanCtrl.notInTheFinancingTimePre");
		} 
	}
}
